// challeng/pages/gift/gift.js
var app = getApp();
var Loading = false;
Page({
	onShareAppMessage:function(){
	    
	  var that = this;
	  var mid = that.data.mid;
	  var sharedata = that.data.shareData;
	  sharedata.path = 'challeng/pages/index/index?mid='+mid+'?gType='+that.data.gametype;
	  return {
		  title: sharedata.title,
	     // desc: sharedata.desc,
	      path: sharedata.path,
	      imageUrl:sharedata.pic,
	      success(res){
          var shareTicket = res.shareTickets[0] // 获取 shareTicket
          wx.getShareInfo({
            shareTicket: shareTicket,
            complete(res) {
              //console.log(res) // 输出加密后的 openGId 信息
              app.util.request({
                url: 'entry/wxapp/addnums',
                data: {
                  m: 'junsion_challeng',
                  t: 1,
                  mid: that.data.openid,
                  iv: res.iv,
                  encryptedData: res.encryptedData,
                },
                success: function (res) {
	    	        	that.setData({
	    	                openZhanji: false
	    	            })
	    	        	if(res.data.code == 1){
	    	        		var challeng_num = parseInt(res.data.num);
	    	        		var mem = that.data.mem;
	    	        		mem.challeng_num = challeng_num;
	    	        		that.setData({ mem: mem});
	    	        		wx.showToast({
                        		title:'次数\t+'+res.data.num,
                        		icon:'none',
                				mask:true,
                				duration:1000
                			});
	    	        	}
	    	        }
	    	    });     
	         }
          });
        },
	      fail: function(res) {
	        // 转发失败
	    	  console.log(res);
	      }
	  }
  },
  data: {
	  openid:'',
	  list:{},
	  page:1,
	  scrollTop : 0,
      scrollHeight : 0,
      gametype:0,
      mid:0,
  },
  
  onLoad: function (options) {
	  var that = this;
	  wx.showLoading({title:'数据加载中...'});
	  var gametype = 0;
  	  var sysType = wx.getStorageSync('gameType');
  	  if(parseInt(sysType)>=0){
  		  gametype = sysType;
  		  that.setData({gametype: gametype});
  	  }
	  app.sysInit(this,function(res){
	  app.getUserInfo(function (res) {
		  var openid = res['openid'];
		//获取分享内容
          app.getShareData(function(res){
          	that.setData({shareData:res.data});
          },openid,gametype);
      	
      	  that.setData({openid:openid});
		  app.util.request({
          	url: 'entry/wxapp/MyGift',
          	data: {
          		m: 'junsion_challeng',
          		t: 1,
          		openid:openid
          	},
          	success: function (res) {
          		wx.hideLoading();
          		that.setData({
          			list : res.data.list,
          			mid : res.data.mid,
          		});
          	},
          });
	  });
	  });
	  wx.setNavigationBarTitle({ title: '奖品记录' });
	  wx.getSystemInfo({
          success:function(res){
              that.setData({
                  scrollHeight:res.windowHeight
              });
          }
      });
  },
  onShow:function(){
    var that = this
    app.getUserInfo(function (info) {
      app.util.request({
        url: 'entry/wxapp/ismem',
        data: { openid: info.openid, m: 'junsion_challeng' },
        success: function (res) {
          if (res.data == 1) {
            wx.navigateBack({});
          }
        },
      });
    });
  },
  goReward:function(e){
	  var that = this;
  	  var dataset = e.currentTarget.dataset;
  	  var id = dataset.id;
	  wx.showLoading({title:'数据加载中...'});
	  wx.redirectTo({
		  url: '../tzfail/tzfail?rid='+id
	  })
  },
  scroll : function(e){
      if(e.detail.scrollTop > 100){
        this.setData({
              floorstatus: true
        });
        }else {
        this.setData({
            floorstatus: false
        });
        }
    },
    //加载更多
    loadMore: function(e) {
      var that = this;
      if (Loading) return;
      Loading = true;
      wx.showLoading();
      app.util.request({
          url: 'entry/wxapp/mygift',
          data: {
              m : 'junsion_challeng',
              t : 1,
              scroll:true,
              page:++that.data.page,
              openid:that.data.openid,
          },
          success: function (res) {
            wx.hideLoading();
            if(res.data.status == 1){
            	Loading = false;
                that.setData({
            	    list: that.data.list.concat(res.data.list),
                });
            }
          },
      });
    }

})